// components/pickerCity/pickerCity.js
const address = require('../../utils/city.js')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        "showPickerCity":{
            type: Boolean,
            observer: function () {
                let self = this;
                self.setData({
                    addressMenuIsShow: true,
                    showModal: true,
                })
            }
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        addressMenuIsShow: false,
        value: [0, 0, 0], //设置联动默认
        provinces: [],
        citys: [],
        areas: [],
        areaInfo: '',
        showModal:false,
    },
    ready:function(){
        // 默认联动显示杭州
        let id = address.provinces[10].id
        this.setData({
            provinces: address.provinces,
            citys: address.citys[id],
            areas: address.areas[address.citys[id][0].id],
        })
        this.setData({
            value: [10, 0, 0], //设置联动默认杭州
        })
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // // 点击所在地区弹出选择框
        // // 执行动画
        startAddressAnimation: function (isShow,val) {
            let self = this;
            if (isShow) {
                self.setData({
                    
                })
            } else {
                self.setData({
                   
                })
            }
            self.triggerEvent('myevent', val);
            self.setData({
                addressMenuIsShow: isShow,
            })
        },
        // 点击地区选择取消按钮
        cityCancel: function (e) {
            
            this.startAddressAnimation(false)
        },
        // 点击地区选择确定按钮
        citySure: function (e) {
            let self = this
            let city = self.data.city
            let value = self.data.value
            // 将选择的城市信息显示到输入框
            let areaInfo = self.data.provinces[value[0]].name + '' + self.data.citys[value[1]].name + '' + self.data.areas[value[2]].name;
            self.startAddressAnimation(false, areaInfo)
            self.setData({
                areaInfo: areaInfo,
            })
        },
        // 处理省市县联动逻辑
        cityChange: function (e) {
            let value = e.detail.value
            let provinces = this.data.provinces
            let citys = this.data.citys
            let areas = this.data.areas
            let provinceNum = value[0]
            let cityNum = value[1]
            let countyNum = value[2]
            // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
            if (this.data.value[0] != provinceNum) {
                let id = provinces[provinceNum].id
                this.setData({
                    value: [provinceNum, 0, 0],
                    citys: address.citys[id],
                    areas: address.areas[address.citys[id][0].id],
                })
            } else if (this.data.value[1] != cityNum) {
                // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
                let id = citys[cityNum].id
                this.setData({
                    value: [provinceNum, cityNum, 0],
                    areas: address.areas[citys[cityNum].id],
                })
            } else {
                // 滑动选择了区
                this.setData({
                    value: [provinceNum, cityNum, countyNum]
                })
            }
        },
    },

})
